<template>
  <div class="container">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item class="school" label="目录名称">
          <el-input v-model="formInline.user" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.region" placeholder="请选择">
            <el-option label="启用" value="shanghai"></el-option>
            <el-option label="禁止" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-button size="small" class="eliminate" @click="formInlineUser"
          >清除</el-button
        >
        <el-button type="primary" size="small" @click="souso">搜索</el-button>
        <el-button
          style="float: right; "
          type="success"
          icon="el-icon-edit"
          size="small"
          @click="dialogVisible = true"
          >新增目录</el-button
        >
      </el-form>
      <el-alert
        class="alert"
        :title="alertText"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="130">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="150">
        </el-table-column>
        <el-table-column prop="directoryName" label="目录名称" width="150">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="150">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="200">
        </el-table-column>
        <el-table-column prop="creatorID" label="面试题数量" width="150">
        </el-table-column>
        <!-- <el-table-column prop="twoLevelDirectory" label="二级目录" width="155">
        </el-table-column> -->
        <el-table-column prop="state" label="状态" width="140">
          <template slot-scope="scope">
            <span>{{ scope.row.state == 1 ? '已启用' : '已禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="operation " label="操作" width="210">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="jinyong">
              <span v-if="scope.row.state == 0">启用</span>
              <span v-else>禁用</span>
            </el-button>
            <el-button
              v-if="scope.row.statys != 'deletad'"
              :disabled="qqwyong || scope.row.state == 1"
              type="text"
              size="small"
              @click="xiugai(scope.row)"
              >修改</el-button
            >
            <el-button
              v-if="scope.row.state != 'deletad'"
              :disabled="qqwyong || scope.row.statys == 1"
              type="text"
              size="small"
              @click="removeById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="qurey.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="qurey.pagesize"
          class="suibian"
          layout="total, prev, pager, next, sizes, jumper"
          :total="20"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-dialog
        @close="ruleForm.name = ''"
        title="新增目录"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <el-form-item label="新增目录">
          <el-select
            v-model="formInline.region"
            placeholder="请选择"
            class="suoshu"
          >
            <el-option
              :label="artem.label"
              value="shanghai"
              v-for="artem in yunwei"
              :key="artem.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input
            v-model="ruleForm.name"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <span slot="footer" class="dialog-footer">
          <el-button @click="quxiao">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>
    </el-form>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-dialog
        title="修改学科"
        :visible.sync="xiugaidialogVisible"
        width="30%"
      >
        <el-form-item label="修改目录">
          <el-select
            v-model="ruleForm.directoryName"
            placeholder="请选择"
            class="suoshu"
          >
            <el-option
              :label="artem.label"
              :value="artem.label"
              v-for="artem in yunwei"
              :key="artem.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input
            v-model="ruleForm.subjectName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <span slot="footer" class="dialog-footer">
          <el-button @click="xiugaidialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="xiugaisure()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import { list, update } from '@/api/hmmm/directorys'
import { simple, add, remove } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      qqwyong: false,
      total: 0,
      alertText: '',
      formInline: {
        user: '',
        region: ''
      },
      tableData: [],
      yunwei: [],
      qurey: {
        page: 1,
        pagesize: 10
      },
      dialogVisible: false,
      xiugaidialogVisible: false,
      ruleForm: {
        directoryName: '',
        subjectID: '',
        id: '',
        subjectName: ''
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur' }
        ]
      },
      value: true
    }
  },

  computed: {
    status () {
      return status
    }
  },
  methods: {
    jinyong () {
      console.log(this.qqwyong)
      this.qqwyong = !this.qqwyong
    },
    async simple () {
      try {
        console.log(123)
        const { data } = await simple()
        console.log(data)
        this.yunwei = data
      } catch (err) {
        console.log(err)
      }
    },
    // async handlStatus(val) {
    //   try {
    //     await changeSata ({
    //       id:val.id,
    //       state: val.state == 1 ? 0 : 1
    //     })
    //     this.getDirectorysList();
    //     this.$message.success("操作成功");
    //   } catch (err) {
    //     this.$message.error("操作失败");
    //   }
    // },
    xiugai (row) {
      console.log(row)
      this.xiugaidialogVisible = true
      this.ruleForm.subjectName = row.subjectName
      this.ruleForm.directoryName = row.directoryName
      this.ruleForm.subjectID = row.subjectID
      this.ruleForm.id = row.id
    },
    async xiugaisure () {
      console.log(this.ruleForm)
      const form = this.ruleForm
      try {
        console.log(123)
        const { data } = await update({
          id: form.id,
          subjectID: form.subjectID,
          directoryName: form.directoryName,
          subjectName: form.subjectName
        })
        console.log(data)
        this.$message('修改成功')
      } catch (err) {
        console.log(err)
      }
      this.xiugaidialogVisible = false
    },
    async sure () {
      try {
        console.log(123)
        const { data } = await add({
          subjectName: this.ruleForm.subjectName,
          isFrontDisplay: 1
        })
        console.log(data)
      } catch (err) {
        console.log(err)
      }
      this.dialogVisible = false
      this.ruleForm.subjectName = ''
      this.formInline.region = ''
    },
    quxiao () {
      this.dialogVisible = false
      this.ruleForm.subjectName = ''
      this.formInline.region = ''
    },
    formInlineUser () {
      this.formInline.user = ''
      this.formInline.region = '请选择'
    },
    async souso () {
      try {
        const { data } = await list({
          ...this.qurey,
          subjectName: this.formInline.user
        })
        console.log(data)
        this.tableData = data.items
      } catch (err) {
        console.log(err)
      }
    },
    onSubmit () {
      console.log('submit!')
    },
    async gitList () {
      try {
        const { data } = await list(this.qurey)
        console.log(data)
        this.tableData = data.items
        this.total = data.counts
        this.alertText = `数据一共是 ${this.total} 条`
      } catch (err) {
        console.log(err)
      }
    },
    handleClick (row) {
      console.log(row)
    },
    handleSizeChange (val) {
      this.qurey.pagesize = val
      this.gitList(this.qurey)
    },
    handleCurrentChange (val) {
      this.qurey.page = val
      this.gitList(this.qurey)
    },
    async removeById (row) {
      console.log(row)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该标签, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await remove({ id: row })
      console.log(res)
      this.$message.success('删除成功！')
      this.gitList()
    }
  },
  created () {
    this.gitList()
    this.simple()
  }
}
</script>

<style scoped>
.school {
  margin-left: 12px;
}
.el-alert {
  margin-bottom: 10px;
}
.suibian {
  display: flex;
  justify-content: flex-end;
}
.block {
  margin-top: 20px;
}
.el-table {
  font-size: 16px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
.suoshu {
  width: 310px;
}
</style>
